package com.orangelife.mobile.parkingcard.adapter;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.curry.android.adapter.FatherAdapter;
import com.curry.android.http.VolleyHttpUtil;
import com.curry.android.util.DialogHelper;
import com.curry.android.util.IntentHelper;
import com.curry.android.util.JSONHelper;
import com.curry.android.util.TimeUtil;
import com.curry.android.util.ToastHelper;
import com.curry.orangelife.mobile.R;
import com.orangelife.mobile.common.activity.OrangeLifeBaseActivity;
import com.orangelife.mobile.common.view.ConfirmDialog;
import com.orangelife.mobile.parkingcard.activity.GrantPermissionActivity;
import com.orangelife.mobile.parkingcard.activity.ParkingSpaceManagementActivity;
import com.orangelife.mobile.parkingcard.activity.ParkingSpaceUsersActivity;
import com.orangelife.mobile.parkingcard.bean.ParkingListBean;
import com.orangelife.mobile.parkingcard.biz.ParkingSpaceHttpBiz;


import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by HeYuCan on 2016/7/5.
 */
public class ParkingSpaceMgmtAdapter extends FatherAdapter {

    private List<ParkingListBean> mList = new ArrayList<>();
    private Context mContext;



//    public ParkingSpaceMgmtAdapter(Context context,String ownerID) {
//        this.mContext = context;
//        this.ownerID=ownerID;
//    }
public ParkingSpaceMgmtAdapter(Context context ) {
    this.mContext = context;
}

    @Override
    public BaseAdapter setList(List list) {
        this.mList = list;
        return null;
    }

    @Override
    public int getCount() {
        return mList == null ? 0 : mList.size();
    }

    @Override
    public Object getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_lv_parking_space_management, null);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        ParkingListBean parkingListBean;
        if (mList != null) {
            parkingListBean = mList.get(position);
            ParkingListBean.UserBean userBean = parkingListBean.getUser();
            String userState = parkingListBean.getUserState();
            String type=parkingListBean.getType();
            String houseInfo = parkingListBean.getHouseInfo();
            String endTime = TimeUtil.timeStampToDate(userBean.getEndTime(), "yyyy/MM/dd");
            String linkAptHouseholderID = userBean.getLinkAptHouseholderID();
            String linkAptHouseholderID1 = parkingListBean.getLinkAptHouseholderID();
            /*???待定key*/
            viewHolder.tvParkingInfo.setText(houseInfo);
            viewHolder.llView.setTag(R.id.ll_view, position);
            viewHolder.tvAuthorization.setTag(R.id.tv_authorize_another_to_use,linkAptHouseholderID1);
            viewHolder.tvAuthorization.setOnClickListener(listener);

            viewHolder.tvTerminateAuthor.setTag(R.id.tv_termination_of_the_authorization, linkAptHouseholderID);
            viewHolder.tvTerminateAuthor.setTag(R.id.btn,position);
            viewHolder.tvTerminateAuthor.setOnClickListener(listener);

            if(type.equals("0")){
                viewHolder.llView.setOnClickListener(listener1);
                viewHolder.ivIcon1.setVisibility(View.VISIBLE);
                viewHolder.ivIcon2.setVisibility(View.VISIBLE);
            }else {
                viewHolder.ivIcon1.setVisibility(View.GONE);
                viewHolder.ivIcon2.setVisibility(View.GONE);
            }
            /*0没有使用人,1有使用人*/
            if (userState.equals("0")) {
                viewHolder.tvAuthorization.setVisibility(View.VISIBLE);
                viewHolder.rlUserName.setVisibility(View.GONE);
                viewHolder.rlContact.setVisibility(View.GONE);
                viewHolder.rlValidUtil.setVisibility(View.GONE);
                viewHolder.tvTerminateAuthor.setVisibility(View.GONE);

            } else if (userState.equals("1")) {
                viewHolder.tvAuthorization.setVisibility(View.GONE);
                viewHolder.rlUserName.setVisibility(View.VISIBLE);
                viewHolder.rlContact.setVisibility(View.VISIBLE);
                viewHolder.rlValidUtil.setVisibility(View.VISIBLE);
                viewHolder.tvTerminateAuthor.setVisibility(View.VISIBLE);

                viewHolder.tvUserName.setText(userBean.getName());
                viewHolder.tvContact.setText(userBean.getPhone());
                viewHolder.tvValidUtil.setText(String.valueOf(endTime));

            }
        }
        return convertView;
    }

    View.OnClickListener listener1 = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            int pos = (int) v.getTag(R.id.ll_view);
            String houseInfo = mList.get(pos).getHouseInfo();
            String apartmentId = mList.get(pos).getApartmentID();
            String linkAptHouseholderID=mList.get(pos).getLinkAptHouseholderID();
            Bundle bundle = new Bundle();
            bundle.putString("houseInfo", houseInfo);
            bundle.putString("apartmentID", apartmentId);
            bundle.putString("linkAptHouseholderID",linkAptHouseholderID);
            IntentHelper.getInstances().bundleWithExtra(mContext, ParkingSpaceUsersActivity.class, bundle);

        }
    };
    View.OnClickListener listener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.tv_authorize_another_to_use:
                    String linkAptHouseholderID=String.valueOf(v.getTag(R.id.tv_authorize_another_to_use));
                    IntentHelper.getInstances().intentWithExtraString(mContext, GrantPermissionActivity.class,"linkAptHouseholderID",linkAptHouseholderID);

                    break;
                case R.id.tv_termination_of_the_authorization:
                    int pos=(int)v.getTag(R.id.btn);
                   String linkAptHouseholderID1=String.valueOf(v.getTag(R.id.tv_termination_of_the_authorization));
                    terminateAuthorization(pos,linkAptHouseholderID1);
                    break;
                default:
                    break;
            }
        }
    };


    private void terminateAuthorization(final int pos,final String linkAptHouseholderID1) {
        final ConfirmDialog confirmDialog = new ConfirmDialog(mContext, "温馨提示", "您确定要终止对该使用者的授权？", "取消", "确定");
        confirmDialog.setClicklistener(new ConfirmDialog.ClickListenerInterface() {
            @Override
            public void doLeft() {
                confirmDialog.dismiss();
            }

            @Override
            public void doRight() {
                confirmDialog.dismiss();
                ((OrangeLifeBaseActivity)mContext).showDialog((OrangeLifeBaseActivity)mContext);
                ParkingSpaceHttpBiz.terminateAuthorization(linkAptHouseholderID1, new VolleyHttpUtil.ResponseCallBack() {
                    @Override
                    public void handleResponse(JSONObject jsonObject, int i) {
                        Map<String, Object> map = new HashMap<>();
                        map = JSONHelper.jsonobjToMap(jsonObject);
                        String errInfo = String.valueOf(map.get("errInfo"));
                        ((OrangeLifeBaseActivity)mContext).closeDialog();
                        if (errInfo.equals("OK")) {
                            mList.get(pos).setUserState("0");
                            notifyDataSetChanged();

                        }else {
                            ToastHelper.getInstance()._toast(errInfo);
                        }
                    }
                });
            }
        });
        confirmDialog.setCanceledOnTouchOutside(false);
        confirmDialog.show();
    }


    class ViewHolder {
        public ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }


        @Bind(R.id.ll_view)
        LinearLayout llView;
        @Bind(R.id.tvType)
        TextView tvType;
        @Bind(R.id.ivIcon1)
        ImageView ivIcon1;
        @Bind(R.id.ivIcon2)
        ImageView ivIcon2;
        @Bind(R.id.tv_parking_info)
        TextView tvParkingInfo;
        @Bind(R.id.rl_user_name)
        RelativeLayout rlUserName;
        @Bind(R.id.tv_user_name)
        TextView tvUserName;
        @Bind(R.id.rl_contact)
        RelativeLayout rlContact;
        @Bind(R.id.tv_contact)
        TextView tvContact;
        @Bind(R.id.rl_valid_util)
        RelativeLayout rlValidUtil;
        @Bind(R.id.tv_valid_util)
        TextView tvValidUtil;
        @Bind(R.id.tv_authorize_another_to_use)
        TextView tvAuthorization;
        @Bind(R.id.tv_termination_of_the_authorization)
        TextView tvTerminateAuthor;

    }

}
